home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / etc / init.d / rc < prev    next >
Encoding:
Text File  |  2009-03-31  |  9.8 KB  |  404 lines

  1. #! /bin/sh
  2. #
  3. # rc
  4. #
  5. # Starts/stops services on runlevel changes.
  6. #
  7. # Optimization: A start script is not run when the service was already
  8. # configured to run in the previous runlevel.  A stop script is not run
  9. # when the the service was already configured not to run in the previous
  10. # runlevel.
  11. #
  12. # Authors:
  13. #     Miquel van Smoorenburg <miquels@cistron.nl>
  14. #     Bruce Perens <Bruce@Pixar.com>
  15.  
  16. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  17. export PATH
  18.  
  19. # Un-comment the following for debugging.
  20. # debug=echo
  21.  
  22. # Specify method used to enable concurrent init.d scripts.
  23. # Valid options are 'none', 'shell' and 'startpar'.  To enable the
  24. # concurrent boot option, the init.d script order must allow for
  25. # concurrency.  This is not the case with the default boot sequence in
  26. # Debian as of 2008-01-20.  Before enabling concurrency, one need to
  27. # check the sequence values of all boot scripts, and make sure only
  28. # scripts that can be started in parallel have the same sequence
  29. # number, and that a scripts dependencies have a earlier sequence
  30. # number. See the insserv package for a away to reorder the boot
  31. # automatically to allow this.
  32. CONCURRENCY=none
  33.  
  34. # Make sure the name survive changing the argument list
  35. scriptname="$0"
  36.  
  37. umask 022
  38.  
  39. on_exit() {
  40.     echo "error: '$scriptname' exited outside the expected code flow."
  41. }
  42. trap on_exit EXIT # Enable emergency handler
  43.  
  44. # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  45. trap ":" INT QUIT TSTP
  46.  
  47. # Set onlcr to avoid staircase effect.
  48. stty onlcr 0>&1
  49.  
  50. # Functions for splash progress bars
  51. if [ -e /lib/init/splash-functions-base ] ; then
  52.     . /lib/init/splash-functions-base
  53. else
  54.     # Quiet down script if old initscripts version without /lib/init/splash-functions-base is used.
  55.     splash_progress() { return 1; }
  56.     splash_stop() { return 1; }
  57. fi
  58.  
  59. # Now find out what the current and what the previous runlevel are.
  60.  
  61. runlevel=$RUNLEVEL
  62. # Get first argument. Set new runlevel to this argument.
  63. [ "$1" != "" ] && runlevel=$1
  64. if [ "$runlevel" = "" ]
  65. then
  66.     echo "Usage: $scriptname <runlevel>" >&2
  67.     exit 1
  68. fi
  69. previous=$PREVLEVEL
  70. [ "$previous" = "" ] && previous=N
  71.  
  72. export runlevel previous
  73.  
  74. . /etc/default/rcS
  75. export VERBOSE
  76.  
  77. if [ -f /lib/lsb/init-functions ] ; then
  78.     . /lib/lsb/init-functions
  79. else
  80.     log_action_msg() { echo $@; }
  81. fi
  82.  
  83. #
  84. # Stub to do progress bar ticks (for splash programs) on startup
  85. #
  86. startup_progress() {
  87.     # Avoid divide by zero if anyone moved xdm/kdm/gdm first in a runlevel.
  88.     if [ 0 -eq "$num_steps" ] ; then return; fi
  89.  
  90.     step=$(($step + $step_change))
  91.     progress=$(($step * $progress_size / $num_steps + $first_step))
  92.     $debug splash_progress "$progress" || true
  93. }
  94.  
  95. sh=sh
  96. # Debian Policy ¬ß9.3.1 requires .sh scripts in runlevel S to be
  97. # sourced However, some important packages currently contain .sh
  98. # scripts that do "exit" at some point, thus killing this process and
  99. # the boot.  Bad!  See also bug #339955.
  100. #[ S = "$runlevel" ] && sh=.
  101.  
  102. #
  103. # Check if we are able to use make like booting.  It require the
  104. # insserv package to be enabled.
  105. #
  106. if [ startpar = "$CONCURRENCY" ] ; then
  107.     test -s /etc/init.d/.depend.boot  || CONCURRENCY="none"
  108.     test -s /etc/init.d/.depend.start || CONCURRENCY="none"
  109.     test -s /etc/init.d/.depend.stop  || CONCURRENCY="none"
  110.     startpar -v      > /dev/null 2>&1 || CONCURRENCY="none"
  111.  
  112.     # startpar do not work properly at the start of rcS.d/.  Avoid it.
  113.     # See #457896 for details.
  114.  
  115.     if [ S = "$runlevel" ] ; then
  116.     CONCURRENCY=none
  117.     fi
  118. fi
  119.  
  120. #
  121. # Start script or program.
  122. #
  123. case "$CONCURRENCY" in
  124.   shell)
  125.       log_action_msg "Using shell-style concurrent boot in runlevel $runlevel"
  126.     startup() {
  127.         action=$1
  128.         shift
  129.         scripts="$@"
  130.         backgrounded=0
  131.         for script in $scripts ; do
  132.             case "$script" in
  133.               *.sh)
  134.                 if [ "." = "$sh" ] ; then
  135.                     RC_SAVE_PATH="$PATH"
  136.                     set -- "$action"
  137.                     $debug . "$script"
  138.                     PATH="$RC_SAVE_PATH"
  139.                     startup_progress
  140.                 else
  141.                     $debug $sh "$script" $action
  142.                     startup_progress
  143.                 fi
  144.                 ;;
  145.               *)
  146.                 $debug "$script" $action &
  147.                 startup_progress
  148.                 backgrounded=1
  149.                 ;;
  150.             esac
  151.         done
  152.         [ 1 = "$backgrounded" ] && wait
  153.     }
  154.     ;;
  155.   startpar)
  156.       log_action_msg "Using startpar-style concurrent boot in runlevel $runlevel"
  157.     startup() {
  158.         action=$1
  159.         shift
  160.         scripts="$@"
  161.         # Make sure .sh scripts are sourced in runlevel S
  162.         if [ "." = "$sh" ] ; then
  163.             newscripts=
  164.             for script in $scripts ; do
  165.                 case "$script" in
  166.                   *.sh)
  167.                     RC_SAVE_PATH="$PATH"
  168.                     set -- "$action"
  169.                     $debug . "$script"
  170.                     PATH="$RC_SAVE_PATH"
  171.                     startup_progress
  172.                     ;;
  173.                   *)
  174.                     newscripts="$newscripts $script"
  175.                     step=$(($step + $step_change))
  176.                     ;;
  177.                 esac
  178.             done
  179.             scripts="$newscripts"
  180.         else
  181.             # Update progress bar counter and jump to the new position
  182.             for script in $scripts ; do
  183.                 step=$(($step + $step_change))
  184.             done
  185.         fi
  186.  
  187.         # startpar is not able to handle time jumps.  So the
  188.         # hwclock.sh scripts should not be executed from
  189.         # within startpar.  The .sh hack above make this
  190.         # problem irrelevant. [pere 2005-09-10]
  191.         [ -n "$scripts" ] && $debug startpar -a $action $scripts
  192.  
  193.         # Jump back one step to compencate for stepping one
  194.         # time too many in the for loop, and to keep the same
  195.         # location as the startup_progress call in the *.sh
  196.         # case.
  197.         step=$(($step - $step_change))
  198.         startup_progress
  199.     }
  200.     ;;
  201.   none|*)
  202.     startup() {
  203.         action=$1
  204.         shift
  205.         scripts="$@"
  206.         for script in $scripts ; do
  207.             case "$script" in
  208.               *.sh)
  209.                 if [ "." = "$sh" ] ; then
  210.                     RC_SAVE_PATH="$PATH"
  211.                     set "$action"
  212.                     $debug . "$script"
  213.                     PATH="$RC_SAVE_PATH"
  214.                     startup_progress
  215.                 else
  216.                     $debug $sh "$script" $action
  217.                     startup_progress
  218.                 fi
  219.                 ;;
  220.               *)
  221.                 $debug "$script" $action
  222.                 startup_progress
  223.                 ;;
  224.             esac
  225.         done
  226.     }
  227.     ;;
  228. esac
  229.  
  230. # Check if the splash screen should be stopped before the given
  231. # script.
  232. is_splash_stop_scripts() {
  233.     scriptname=$1
  234.     case "$scriptname" in
  235.     # killprocs is used in runlevel 1
  236.     gdm|xdm|kdm|ltsp-client|ltsp-client-core|reboot|halt|killprocs)
  237.         return 0
  238.         ;;
  239.     esac
  240.     return 1
  241. }
  242.  
  243. # Is there an rc directory for this new runlevel?
  244. if [ -d /etc/rc$runlevel.d ]
  245. then
  246.     # Find out where in the progress bar the initramfs got to.
  247.     PROGRESS_STATE=0
  248.     if [ -f /dev/.initramfs/progress_state ]; then
  249.         . /dev/.initramfs/progress_state
  250.     fi
  251.  
  252.     # Split the remaining portion of the progress bar into thirds
  253.     progress_size=$(((100 - $PROGRESS_STATE) / 3))
  254.  
  255.     case "$runlevel" in
  256.         0|6)
  257.             ACTION=stop
  258.             # Count down from 0 to -100 and use the entire bar
  259.             first_step=0
  260.             progress_size=100
  261.             step_change=-1
  262.             ;;
  263.             S)
  264.                 ACTION=start
  265.             # Begin where the initramfs left off and use 2/3
  266.             # of the remaining space
  267.             first_step=$PROGRESS_STATE
  268.             progress_size=$(($progress_size * 2))
  269.             step_change=1
  270.             ;;
  271.         *)
  272.             ACTION=start
  273.             # Begin where rcS left off and use the final 1/3 of
  274.             # the space (by leaving progress_size unchanged)
  275.             first_step=$(($progress_size * 2 + $PROGRESS_STATE))
  276.             step_change=1
  277.             ;;
  278.     esac
  279.  
  280.     # Count the number of scripts we need to run
  281.     # (for progress bars)
  282.     num_steps=0
  283.     for s in /etc/rc$runlevel.d/[SK]*; do
  284.         if is_splash_stop_scripts "${s##/etc/rc$runlevel.d/S??}" ; then
  285.         break
  286.         fi
  287.         num_steps=$(($num_steps + 1))
  288.     done
  289.     step=0
  290.  
  291.     # First, run the KILL scripts.
  292.     if [ "$previous" != N ]
  293.     then
  294.         # Run all scripts with the same level in parallel
  295.         CURLEVEL=""
  296.         for s in /etc/rc$runlevel.d/K*
  297.         do
  298.             # Extract order value from symlink
  299.             level=${s#/etc/rc$runlevel.d/K}
  300.             level=${level%%[a-zA-Z]*}
  301.             if [ "$level" = "$CURLEVEL" ]
  302.             then
  303.                 continue
  304.             fi
  305.             CURLEVEL=$level
  306.             SCRIPTS=""
  307.             for i in /etc/rc$runlevel.d/K$level*
  308.             do
  309.                 # Check if the script is there.
  310.                 [ ! -f $i ] && continue
  311.  
  312.                 #
  313.                 # Find stop script in previous runlevel but
  314.                 # no start script there.
  315.                 #
  316.                 suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
  317.                 previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  318.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  319.                 #
  320.                 # If there is a stop script in the previous level
  321.                 # and _no_ start script there, we don't
  322.                 # have to re-stop the service.
  323.                 #
  324.                 [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  325.  
  326.                 # Stop the service.
  327.                 SCRIPTS="$SCRIPTS $i"
  328.                 if is_splash_stop_scripts "$suffix" ; then
  329.                     splash_stop || true
  330.                 fi
  331.             done
  332.             startup stop $SCRIPTS
  333.         done
  334.     fi
  335.  
  336.     # Now run the START scripts for this runlevel.
  337.     # Run all scripts with the same level in parallel
  338.     CURLEVEL=""
  339.     for s in /etc/rc$runlevel.d/S*
  340.     do
  341.         # Extract order value from symlink
  342.         level=${s#/etc/rc$runlevel.d/S}
  343.         level=${level%%[a-zA-Z]*}
  344.         if [ "$level" = "$CURLEVEL" ]
  345.         then
  346.             continue
  347.         fi
  348.         CURLEVEL=$level
  349.         SCRIPTS=""
  350.         for i in /etc/rc$runlevel.d/S$level*
  351.         do
  352.             [ ! -f $i ] && continue
  353.  
  354.             suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
  355.             if [ "$previous" != N ]
  356.             then
  357.                 #
  358.                 # Find start script in previous runlevel and
  359.                 # stop script in this runlevel.
  360.                 #
  361.                 stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
  362.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  363.                 #
  364.                 # If there is a start script in the previous level
  365.                 # and _no_ stop script in this level, we don't
  366.                 # have to re-start the service.
  367.                 #
  368.                 if [ start = "$ACTION" ] ; then
  369.                     [ -f $previous_start ] && [ ! -f $stop ] && continue
  370.                 else
  371.                     # Workaround for the special
  372.                     # handling of runlevels 0 and 6.
  373.                     previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  374.                     #
  375.                     # If there is a stop script in the previous level
  376.                     # and _no_ start script there, we don't
  377.                     # have to re-stop the service.
  378.                     #
  379.                     [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  380.                 fi
  381.  
  382.             fi
  383.             SCRIPTS="$SCRIPTS $i"
  384.             if is_splash_stop_scripts "$suffix" ; then
  385.                 splash_stop || true
  386.             fi
  387.         done
  388.         startup $ACTION $SCRIPTS
  389.     done
  390. fi
  391.  
  392. if [ S = "$runlevel" ]
  393. then
  394.     #
  395.     # For compatibility, run the files in /etc/rc.boot too.
  396.     #
  397.     [ -d /etc/rc.boot ] && run-parts /etc/rc.boot
  398. fi
  399.  
  400. trap - EXIT # Disable emergency handler
  401.  
  402. exit 0
  403.  
  404.